package com.cwd.arm.inter.controller;

import com.cwd.arm.application.dto.ReceiveNodeDto;
import com.cwd.arm.application.service.read.facade.ReceiveNodeReadService;
import com.cwd.arm.application.service.write.facade.ReceiveNodeWriteService;
import com.cwd.arm.inter.query.ReceiveNodeQuery;
import com.cwd.arm.inter.util.DtoToVoUtil;
import com.cwd.arm.inter.util.QueryToDaoQueryUtil;
import com.cwd.arm.inter.util.VoToDtoUtil;
import com.cwd.arm.inter.vo.ResponseVo;
import com.cwd.arm.inter.vo.receiveNode.ReceiveNodeAddVo;
import com.cwd.arm.inter.vo.receiveNode.ReceiveNodeUpdateVo;
import com.cwd.arm.inter.vo.receiveNode.ReceiveNodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Description:收款节点
 * Created on 2021年11月16日
 *
 * @author yan
 */
@Api(value = "收款节点接口", description = "收款节点接口")
@RestController
@RequestMapping(value = "/receiveNode")
public class ReceiveNodeController {

    @Autowired
    private ReceiveNodeReadService readService;

    @Autowired
    private ReceiveNodeWriteService writeService;

    @ApiOperation(value = "查询收款节点信息", notes = "查询收款节点信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/{id}")
    public ResponseVo<ReceiveNodeVo> getReceiveNode(@PathVariable Long id) {
        ReceiveNodeDto dto = readService.selectById(id);
        return ResponseVo.<ReceiveNodeVo>BUILDER().success(convertToVo(dto));
    }

    @ApiOperation(value = "查询收款节点列表", notes = "查询收款节点列表")
    @GetMapping("")
    public ResponseVo<List<ReceiveNodeVo>> getReceiveNodeList(ReceiveNodeQuery query) {
        List<ReceiveNodeDto> dtoList = readService.selectByQuery(QueryToDaoQueryUtil.toDaoQuery(query));
        return ResponseVo.<List<ReceiveNodeVo>>BUILDER().success(DtoToVoUtil.convertToReceiveNodeVoList(dtoList));
    }

    @ApiOperation(value = "新增收款节点", notes = "新增收款节点")
    @Secured({"ROLE_NODE_ADD"})
    @PostMapping("")
    public ResponseVo<String> add(@RequestBody ReceiveNodeAddVo addVo) {
        writeService.save(VoToDtoUtil.convertToDto(addVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "修改收款节点", notes = "修改收款节点")
    @Secured({"ROLE_NODE_EDIT"})
    @PutMapping("")
    public ResponseVo<String> update(@RequestBody ReceiveNodeUpdateVo updateVo) {
        writeService.update(VoToDtoUtil.convertToDto(updateVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "删除收款节点", notes = "删除收款节点")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "收款节点id", required = true, dataType = "Long", paramType = "path")
    })
    @Secured({"ROLE_NODE_DEL"})
    @DeleteMapping("/{id}")
    public ResponseVo<String> del(@PathVariable Long id) {
        writeService.del(id);
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "收款节点百分比转金额", notes = "收款节点百分比转金额")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "rate", value = "值,没有项目id时", required = true, dataType = "BigDecimal"),
            @ApiImplicitParam(name = "nodeId", value = "节点id,修改节点时必传", dataType = "Long"),
            @ApiImplicitParam(name = "date", value = ",收款截止日期,修改节点时必传", dataType = "Long"),
    })
    @GetMapping("/amount")
    public ResponseVo<BigDecimal> getAmount(Long projectId, BigDecimal rate, Long nodeId,String date) throws Exception{
        return ResponseVo.<BigDecimal>BUILDER().success(readService.getAmount(projectId, nodeId, rate,date));
    }

    @ApiOperation(value = "收款节点金额转百分比", notes = "收款节点百分比转金额")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "amount", value = "节点金额", required = true, dataType = "BigDecimal"),
            @ApiImplicitParam(name = "nodeId", value = "节点id,修改节点时必传", dataType = "Long"),
            @ApiImplicitParam(name = "date", value = ",收款截止日期,修改节点时必传", dataType = "Long"),
    })
    @GetMapping("/rate")
    public ResponseVo<BigDecimal> getRate(Long projectId, BigDecimal amount, Long nodeId,String date) throws Exception{
        return ResponseVo.<BigDecimal>BUILDER().success(readService.getRate(projectId, nodeId, amount,date));
    }

    private ReceiveNodeVo convertToVo(ReceiveNodeDto dto) {
        ReceiveNodeVo vo = new ReceiveNodeVo();
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

}
